<?php

namespace App\Traits;

use App\Services\AuthService;

trait ModelEventTrait
{
    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            /* @var $model \Illuminate\Database\Eloquent\Model */

            $model->create_by_id = AuthService::getInstance()->getAdminId();
            $model->create_by = AuthService::getInstance()->getAdminName();
        });

        static::updating(function ($model) {
            /* @var $model \Illuminate\Database\Eloquent\Model */
            $model->update_by_id = AuthService::getInstance()->getAdminId();
            $model->update_by = AuthService::getInstance()->getAdminName();
        });
    }
    public static function insertAndGetId(array $attributes)
    {
        $attributes['create_by_id'] = AuthService::getInstance()->getAdminId();
        $attributes['create_by'] = AuthService::getInstance()->getAdminName();
        return static::insertGetId($attributes);
    }
}
